package com.smugmugsoftware.airconcalc.advanced;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.content.DialogInterface.OnClickListener;
import android.widget.TextView;

public class ConfirmDialog extends DialogFragment {

	public static final int DELETE = 50;
	public static final int NO_ROOM_NAME = 51;
	public static final int NO_ROOM_LOAD = 52;	
	public static final int NO_COMPONENT_LOAD = 53;	
	public static final int DISCARD_CHANGES = 54;	

	private MainActivityInterface activity;

	private Context context;
	private DialogFragment thisDialog;
	private int type;
	private int dialogType;
	private long id;

	public ConfirmDialog() {}

	public ConfirmDialog(Context context, int dialogType, int type, long id) {
		this.context = context;
		this.id = id;
		this.type = type;
		this.dialogType = dialogType;
		this.thisDialog = this;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRetainInstance(true);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			this.activity = (MainActivityInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		final AlertDialog.Builder builder = new AlertDialog.Builder(context);

		TextView dialogView = new TextView(context);
		dialogView.setPadding(12, 12, 12, 12);
		dialogView.setGravity(Gravity.CENTER);
		dialogView.setTextColor(getResources().getColor(R.color.off_white));
		dialogView.setTextSize(16);
		builder.setTitle("Confirm");

		OnClickListener positiveListener = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				thisDialog.dismiss();

				activity.returnFromConfirmDialog(dialogType, type, id);
			}
		};

		OnClickListener negativeListener = new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				thisDialog.dismiss();
			}
		};

		if (dialogType == DELETE) {
			dialogView.setText("Delete this item?");
			builder.setPositiveButton("DELETE", positiveListener);
			builder.setNegativeButton("Cancel", negativeListener);

		} else if (dialogType == DISCARD_CHANGES) {
			dialogView.setText("Changes will be discarded.");
			builder.setPositiveButton("OK", positiveListener);
			builder.setNegativeButton("Cancel", negativeListener);

		} else if (dialogType == NO_COMPONENT_LOAD) {
			dialogView.setText("### no component load message ###");
			builder.setNegativeButton("Ok", negativeListener);

		} else if (dialogType == NO_ROOM_LOAD) {
			dialogView.setText("This room has an expected cooling load of 0 BTU.  Save anyway?");
			builder.setPositiveButton("YES", positiveListener);
			builder.setNegativeButton("No", negativeListener);

		} else if (dialogType == NO_ROOM_NAME) {
			dialogView.setText("This room has no name.  Generate a generic name?");
			builder.setPositiveButton("YES", positiveListener);
			builder.setNegativeButton("No", negativeListener);

		}
		builder.setView(dialogView);
		return builder.create();
	}
}